﻿using System;
using System.IO;
using System.Windows;

namespace Sinacor.OMM.Deployment.SetupExtrator
{
	public partial class Extrator
    {
        #region [ Constructor ]

        public Extrator()
		{
			this.InitializeComponent();
        }

        #endregion

        #region [ Events ]

        private void btnSearchDirectory_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folderPicker = new System.Windows.Forms.FolderBrowserDialog();
            folderPicker.Description = "Selecione o diretório para a extração dos arquivos.";
            folderPicker.ShowDialog();

            if (folderPicker.SelectedPath != string.Empty)
            {
                //txtDirectory.Text = folderPicker.SelectedPath;
                //btnExtractDirectory.IsEnabled = true;
            }
        }        

        private void ExtratorScreen_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (ShowExitConfirm())
                CloseMainWindow();
            else
                e.Cancel = true;
        }

        private void ExtratorScreen_ClosingEnd(object sender, System.ComponentModel.CancelEventArgs e) { }

        private void btnExtractDirectory_GotFocus(object sender, RoutedEventArgs e)
        {
            //btnExtractDirectory.IsEnabled = false;
            //btnSearchDirectory.IsEnabled = false;
            //lblExtractingFiles.Visibility = Visibility.Visible;

            //string directory = txtDirectory.Text;
            //System.Threading.Thread thread = new System.Threading.Thread(delegate() { ExtractFiles(directory); });
            //thread.Start();
        }

        #endregion

        #region [ Methods ]

        private bool ShowExitConfirm()
        {
            if (MessageBox.Show("Deseja realmente sair do Setup?", "Setup OMM", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
                return true;
            return false;
        }

        private void ExtractFiles(string directory)
        {
            string currentDirectory = Environment.CurrentDirectory;
            string copyDirectory = currentDirectory + @"\OrderMessageManager";
            string destDirectory = directory + @"\OrderMessageManager";

            CreateDirectory(destDirectory);
            CopyDirectory(copyDirectory, destDirectory);            

            RunSetup(destDirectory);
            CloseMainWindow();
        }

        private void CloseMainWindow()
        {
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        private void RunSetup(string destDirectory)
        {
            string setupExe = destDirectory + @"\" + "Sinacor.OMM.Deployment.Setup.exe";

            if (System.IO.File.Exists(setupExe))
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = setupExe;                                
                process.Start();                
            }
        }

        private void CreateDirectory(string destDirectory)
        {
            if (Directory.Exists(destDirectory))
                Directory.Delete(destDirectory, true);

            Directory.CreateDirectory(destDirectory);
        }

        private void CopyDirectory(string copyDirectory, string destDirectory)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(copyDirectory);
            FileInfo[] files = dirInfo.GetFiles("*", SearchOption.TopDirectoryOnly);
            foreach (FileInfo file in files)
            {
                file.IsReadOnly = false;
                file.CopyTo(destDirectory + @"\" + file.Name);
            }

            DirectoryInfo[] dirInfoChildren = dirInfo.GetDirectories();
            foreach (DirectoryInfo children in dirInfoChildren)
            {
                CreateDirectory(destDirectory + @"\" + children.Name);
                CopyDirectory(children.FullName, destDirectory + @"\" + children.Name);
            }
        }

        #endregion        
	}
}